<?php
/**
 * Created by PhpStorm.
 * User: MG
 * Date: 2020/9/21
 * Time: 16:40
 */

namespace app\common\service;

class Location
{
    /**
     * 计算两个经纬度之间的距离  单位 米
     * @param $lng1
     * @param $lat1
     * @param $lng2
     * @param $lat2
     * @return float|int
     */
    public static function getdistance($lng1, $lat1, $lng2, $lat2) {
        // 将角度转为狐度
        $radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($lng1);
        $radLng2 = deg2rad($lng2);
        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
        return $s;
    }

    /**
     * 根据经纬度获取城市名称
     * @param $longitude
     * @param $latitude
     * @return mixed|string
     */
    public static function cityName($longitude,$latitude)
    {
        $key=get_addon_config('maptype')['tencentkey'];
        //调取腾讯接口,其中ak为key,注意location纬度在前，经度在后
        $api = "https://apis.map.qq.com/ws/geocoder/v1/?location=" . $latitude . "," . $longitude . "&output=json&pois=1&key=$key";
        $content = file_get_contents($api);
        $arr = json_decode($content, true);
        if ($arr['status'] == 0) {
            return $arr;
        } else {
            return 'error';
        }
    }

    //根据地址获取经纬度
    public static function queryAddress($address)
    {
        $key =get_addon_config('maptype')['tencentkey']; //腾讯地图开发平台自己申请
        $url = 'https://apis.map.qq.com/ws/geocoder/v1/?address='.$address.'&key='.$key;
        $info = file_get_contents($url);
        $info = json_decode($info, true);
        return $info;

    }
}